//
//  AppDelegate.m
//  <5>键值编码和键值对
//
//  Created by chen on 16/9/7.
//  Copyright © 2016年 tyrad. All rights reserved.
//

#import "AppDelegate.h"
#import "Person.h" 

@interface AppDelegate ()

@property(nonatomic)int fido;

@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTextField *label;
@property (weak) IBOutlet NSSliderCell *slider;

@end

@implementation AppDelegate

- (void)setFido:(int)fido{
    _fido = fido ;
    NSLog(@"%@",@(fido));
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setValue:[NSNumber numberWithDouble:1.999] forKey:@"fido"];
        
        //KVC机制将NSNumber自动转为Int
        NSNumber *number = [self valueForKey:@"fido"];
        NSLog(@"%@",number);
        
    }
    return self;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
    //举个栗子
    Person *p1 = [[Person alloc]initWithAge:10];
    Person *p2 = [[Person alloc]initWithAge:11];
    Person *p3 = [[Person alloc]initWithAge:12];

    NSNumber *number = [@[p1, p2 , p3] valueForKeyPath:@"@avg.age"];
    NSLog(@"age = %@",number); //得到 "age = 11"
    
}



- (IBAction)increase:(id)sender {
    
    [self willChangeValueForKey:@"fido"];
    _fido++ ;
    [self didChangeValueForKey:@"fido"];
    
    
    /** 除了这种写法还可以 
     
     1. setValue:forKey: 
     2. [self setFido: [self fido] +1 ]
     */
}


@end









